<?php
/**
*
* @package shopBB
* @copyright (c) 2008 EXreaction, Lithium Studios
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

class acp_shop
{
	var $u_action;
	var $new_config = array();
	var $parent_id = 0;

	function main($id, $mode)
	{
		global $phpbb_root_path, $phpEx, $user;

		if (!defined('PHPBB_ROOT_PATH')) define('PHPBB_ROOT_PATH', $phpbb_root_path);
		if (!defined('SHOPBB_PATH')) define('SHOPBB_PATH', PHPBB_ROOT_PATH . 'shop/');
		if (!defined('PHP_EXT')) define('PHP_EXT', $phpEx);

		include(SHOPBB_PATH . 'constants.' . PHP_EXT);
		include(SHOPBB_PATH . 'shop_class.' . PHP_EXT);
		include(SHOPBB_PATH . 'functions.' . PHP_EXT);
		include(SHOPBB_PATH . 'plugins/plugins.' . PHP_EXT);

		new shop();
		new shop_plugins();

		shop::add_lang(array('shop', 'acp_shop'));

		$submit = (isset($_POST['submit'])) ? true : false;

		// Check Form Key
		add_form_key('acp_shop');
		if ($submit && !check_form_key('acp_shop'))
		{
			trigger_error($user->lang['FORM_INVALID'] . adm_back_link($this->u_action), E_USER_WARNING);
		}

		switch($mode)
		{
			case 'plugins' :
				$this->plugins($id, $mode);
			break;
			default :
				$default = true;
				extract(shop_plugins::plugin_do('acp_default', compact('default', 'id', 'mode'))); // make sure to set default to false if you use your own page

				if ($default)
				{
					$this->settings($id, $mode);
				}
		}

		shop_plugins::plugin_do('acp_end');
	}

	function settings($id, $mode)
	{
		global $config, $db, $user, $auth, $template;

		$submit = (isset($_POST['submit'])) ? true : false;
		$action = request_var('action', '');

		$this->tpl_name = 'acp_board';
		$this->page_title = $user->lang['ACP_SHOP'];

		shop_plugins::plugin_do('acp_main_start');

		$settings = array(
			'legend0'					=> 'SHOP_VERSION',
			'shopbb_version'			=> array('lang' => 'SHOP_VERSION',			'type' => 'custom',		'method' => 'shopbb_version',			'explain' => false),

			'legend1'					=> 'SHOP_SETTINGS',
			'shop_enabled'				=> array('lang' => 'ENABLE_SHOP',			'validate' => 'bool',	'type' => 'radio:enabled_disabled',		'explain' => true),
			'shop_seo'					=> array('lang' => 'SHOP_SEO',				'validate' => 'bool',	'type' => 'radio:enabled_disabled',		'explain' => true),

			'legend2'					=> 'CURRENCY_SETTINGS',
			'shop_currency'				=> array('lang' => 'SHOP_CURRENCY',			'type' => 'custom',		'method' => 'shop_currency',			'explain' => true),
			'shop_buy_currency'			=> array('lang' => 'SHOP_BUY_CURRENCY',		'validate' => 'bool',	'type' => 'radio:enabled_disabled',		'explain' => true),
			'shop_sell_currency'		=> array('lang' => 'SHOP_SELL_CURRENCY',	'validate' => 'bool',	'type' => 'radio:enabled_disabled',		'explain' => true),
		);

		shop_plugins::plugin_do_ref('acp_main_settings', $settings);

		$this->new_config = $config;
		$cfg_array = (isset($_REQUEST['config'])) ? utf8_normalize_nfc(request_var('config', array('' => ''), true)) : $this->new_config;
		$error = array();

		// We validate the complete config if whished
		validate_config_vars($settings, $cfg_array, $error);

		// Do not write values if there is an error
		if (sizeof($error))
		{
			$submit = false;
		}

		$template->assign_vars(array(
			'L_TITLE'			=> $user->lang['SHOP_SETTINGS'],
			'L_TITLE_EXPLAIN'	=> $user->lang['SHOP_SETTINGS_EXPLAIN'],

			'S_ERROR'			=> (sizeof($error)) ? true : false,
			'ERROR_MSG'			=> implode('<br />', $error),

			'U_ACTION'			=> $this->u_action,
		));

		foreach ($settings as $config_key => $vars)
		{
			if ($submit)
			{
				if (!isset($cfg_array[$config_key]) || strpos($config_key, 'legend') !== false)
				{
					continue;
				}

				$this->new_config[$config_key] = $config_value = $cfg_array[$config_key];

				set_config($config_key, $config_value);
			}
			else
			{
				if (!is_array($vars) && strpos($config_key, 'legend') === false)
				{
					continue;
				}

				if (strpos($config_key, 'legend') !== false)
				{
					$template->assign_block_vars('options', array(
						'S_LEGEND'		=> true,
						'LEGEND'		=> (isset($user->lang[$vars])) ? $user->lang[$vars] : $vars)
					);

					continue;
				}

				$type = explode(':', $vars['type']);

				$l_explain = '';
				if ($vars['explain'] && isset($vars['lang_explain']))
				{
					$l_explain = (isset($user->lang[$vars['lang_explain']])) ? $user->lang[$vars['lang_explain']] : $vars['lang_explain'];
				}
				else if ($vars['explain'])
				{
					$l_explain = (isset($user->lang[$vars['lang'] . '_EXPLAIN'])) ? $user->lang[$vars['lang'] . '_EXPLAIN'] : '';
				}

				$template->assign_block_vars('options', array(
					'KEY'			=> $config_key,
					'TITLE'			=> (isset($user->lang[$vars['lang']])) ? $user->lang[$vars['lang']] : $vars['lang'],
					'S_EXPLAIN'		=> $vars['explain'],
					'TITLE_EXPLAIN'	=> $l_explain,
					'CONTENT'		=> build_cfg_template($type, $config_key, $this->new_config, $config_key, $vars),
					)
				);
			}
		}

		if ($submit)
		{
			add_log('admin', 'LOG_SHOP_CONFIG');

			trigger_error($user->lang['CONFIG_UPDATED'] . adm_back_link($this->u_action));
		}
	}

	function shopbb_version($value, $key)
	{
		global $user, $config, $cache;

		$version = $user->lang['DATABASE_VERSION'] . ': ' . $value . '<br />';
		$version .= $user->lang['LATEST_VERSION'] . ': ';

		$latest_version = $cache->get('shopbb_version');
		if ($latest_version === false)
		{
			if (!function_exists('get_remote_file'))
			{
				include(PHPBB_ROOT_PATH . 'includes/functions_admin.' . PHP_EXT);
			}

			$errstr = $errno = '';
			$latest_version = get_remote_file('lithiumstudios.org', '/updatecheck', 'shopbb.txt', $errstr, $errno);

			$cache->put('shopbb_version', $latest_version, 3600);
		}

		if ($latest_version === false)
		{
			$version .= $user->lang['NOT_AVAILABLE'];
			$version .= '<br />' . sprintf($user->lang['CLICK_CHECK_NEW_VERSION'], '<a href="http://www.lithiumstudios.org/forum/viewtopic.php?f=41&t=433">', '</a>');
		}
		else
		{
			$version .= $latest_version;
			if (version_compare($file_version, $latest_version, '<'))
			{
				$version .= '<br />' . sprintf($user->lang['CLICK_GET_NEW_VERSION'], '<a href="http://www.lithiumstudios.org/forum/viewtopic.php?f=41&t=433">', '</a>');
			}
		}

		return $version;
	}

	function shop_currency($value, $key)
	{
		$select = "<select name=\"{$key}\" id=\"{$key}\">";

	    $dh = @opendir(SHOPBB_PATH . 'currency/');
		if ($dh)
		{
			while (($file = readdir($dh)) !== false)
			{
				if (strpos($file, PHP_EXT) !== false)
				{
					include(SHOPBB_PATH . 'currency/' . $file);

					$class = substr($file, 0, -(strlen(PHP_EXT) + 1));
					$currency = new $class;
					if ($currency->is_installed())
					{
						//$select .= "<option value=\"{$class}{$option}\">" . $option_name . '</option>';
					}
				}
			}
			closedir($dh);
		}
		$select .= '</select>';
		return $select;
	}

	function plugins($id, $mode)
	{
		global $config, $db, $user, $auth, $template;

		if (shop_plugins::load_all_plugins() === false)
		{
			trigger_error('PLUGINS_DISABLED');
		}

		$submit = (isset($_POST['submit'])) ? true : false;
		$action = request_var('action', '');
		$action_to = request_var('name', '');

		$this->tpl_name = 'acp_shop_plugins';
		$this->page_title = 'ACP_SHOP_PLUGINS';

		$template->assign_vars(array(
			'U_ACTION'			=> $this->u_action,
		));

		switch ($action)
		{
			case 'activate' :
				shop_plugins::plugin_enable($action_to);
			break;
			case 'deactivate' :
				shop_plugins::plugin_disable($action_to);
			break;
			case 'install' :
				shop_plugins::plugin_install($action_to);
			break;
			case 'uninstall' :
				if (confirm_box(true))
				{
					shop_plugins::plugin_uninstall($action_to);
				}
				else
				{
					confirm_box(false, 'PLUGIN_UNINSTALL');
				}
			break;
			case 'update' :
				shop_plugins::plugin_update($action_to);
			break;
			case 'move_up' :
			case 'move_down' :
				shop_plugins::plugin_move($action_to, $action);

				// We need to do a redirect here because the plugins list is not shown correctly after one is moved...and I see no easy way to resync the list
				redirect($this->u_action);
			break;
		}

		$i = 0;
		$installed_plugins = array();
		foreach (shop_plugins::$available_plugins as $name => $data)
		{
			$i++;
			$installed = (array_key_exists($name, shop_plugins::$plugins)) ? true : false;
			$active = ($installed && shop_plugins::$plugins[$name]['plugin_enabled']) ? true : false;

			$s_actions = array();
			if ($installed)
			{
				if ($active)
				{
					$s_actions[] = '<a href="' . $this->u_action . "&amp;action=deactivate&amp;name=" . $name . '">' . $user->lang['PLUGIN_DEACTIVATE'] . '</a>';
					$s_actions[] = '<a href="' . $this->u_action . "&amp;action=uninstall&amp;name=" . $name . '">' . $user->lang['PLUGIN_UNINSTALL'] . '</a>';
				}
				else
				{
					$s_actions[] = '<a href="' . $this->u_action . "&amp;action=activate&amp;name=" . $name . '">' . $user->lang['PLUGIN_ACTIVATE'] . '</a>';
					$s_actions[] = '<a href="' . $this->u_action . "&amp;action=uninstall&amp;name=" . $name . '">' . $user->lang['PLUGIN_UNINSTALL'] . '</a>';
				}

				if ($i > 1)
				{
					$s_actions[] = '<a href="' . $this->u_action . "&amp;action=move_up&amp;name=" . $name . '">' . $user->lang['MOVE_UP'] . '</a>';
				}
				if ($i != sizeof(shop_plugins::$plugins))
				{
					$s_actions[] = '<a href="' . $this->u_action . "&amp;action=move_down&amp;name=" . $name . '">' . $user->lang['MOVE_DOWN'] . '</a>';
				}

				if ($data['plugin_version'] != shop_plugins::$plugins[$name]['plugin_version'])
				{
					$version = array('files' => explode('.', $data['plugin_version']), 'db' => explode('.', shop_plugins::$plugins[$name]['plugin_version']));

					$i = 0;
					$newer_files = false;
					foreach ($version['files'] as $v)
					{
						if ($v > $version['db'][$i])
						{
							$newer_files = true;
							break;
						}
						else if ($v < $version['db'][$i])
						{
							break;
						}
						$i++;
					}
					if ($newer_files)
					{
						$s_actions[] = '<a href="' . $this->u_action . "&amp;action=update&amp;name=" . $name . '">' . $user->lang['PLUGIN_UPDATE'] . '</a>';
					}
				}
			}
			else
			{
				$s_actions[] = '<a href="' . $this->u_action . "&amp;action=install&amp;name=" . $name . '">' . $user->lang['PLUGIN_INSTALL'] . '</a>';
			}

			if ($installed)
			{
				$installed_plugins[$name] = array(
					'NAME'				=> (isset($data['plugin_title'])) ? $data['plugin_title'] : $name,
					'DESCRIPTION'		=> (isset($data['plugin_description'])) ? $data['plugin_description'] : '',
					'S_ACTIONS'			=> implode(' | ', $s_actions),
					'COPYRIGHT'			=> (isset($data['plugin_copyright'])) ? $data['plugin_copyright'] : '',
					'DATABASE_VERSION'	=> ($installed) ? shop_plugins::$plugins[$name]['plugin_version'] : false,
					'FILES_VERSION'		=> (isset($data['plugin_version'])) ? $data['plugin_version'] : '',
				);
			}
			else
			{
				$template->assign_block_vars('uninstalled', array(
					'NAME'				=> (isset($data['plugin_title'])) ? $data['plugin_title'] : $name,
					'DESCRIPTION'		=> (isset($data['plugin_description'])) ? $data['plugin_description'] : '',
					'S_ACTIONS'			=> implode(' | ', $s_actions),
					'COPYRIGHT'			=> (isset($data['plugin_copyright'])) ? $data['plugin_copyright'] : '',
					'DATABASE_VERSION'	=> ($installed) ? shop_plugins::$plugins[$name]['plugin_version'] : false,
					'FILES_VERSION'		=> (isset($data['plugin_version'])) ? $data['plugin_version'] : '',
				));
			}
		}

		foreach (shop_plugins::$plugins as $name => $row)
		{
			$template->assign_block_vars('installed', $installed_plugins[$name]);
		}
	}
}
?>